package com.alex.megacj.handler;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;

import com.alex.utils.http.HttpResult;

public class ProxyHandlerUtils {

	public static void parseUrl(RequestContext context) throws MalformedURLException {
		StringBuilder sb = new StringBuilder("http://");
		String path = context.request.getRequestURI().substring(context.request.getContextPath().length());
		sb.append(context.targetHost).append(path);
		if (context.request.getQueryString() != null) {
			sb.append("?").append(context.request.getQueryString());
		}
		context.targetUrl = sb.toString();
		context.targetUrlParsed = new URL(context.targetUrl);
	}

	public static String formatHeader(String name) {
		String[] parts = StringUtils.split(name, "-");
		for (int i = 0; i < parts.length; i++) {
			parts[i] = StringUtils.capitalize(parts[i]);
		}
		return StringUtils.join(parts, "-");
	}

	public static boolean isHTML(HttpResult httpResult) {
		Header contentType = httpResult.getResponse().getFirstHeader("content-type");
		return contentType != null && contentType.getValue().toLowerCase().contains("html");
	}
}
